//
//  WLUserModel.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/5/24.
//

import UIKit
import SwiftyJSON

class WLUserModel: NSObject {

    //获得业务员手机号
    class func getPhone() -> String {
        let str:String = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_Phone) ?? ""
        return str.count > 0 ? str : "";
    }

    //获得业务员编号
    class func getEMPId() -> String {
        let str:String = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_UserId) ?? ""
        return str.count > 0 ? str : "";
    }

    class func getAvatarPath() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_AvatarPath) ?? ""
        return str.count > 0 ? str : ""
    }

    class func getEmail() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_Email) ?? ""
        return str.count > 0 ? str : ""
    }

    class func getGender() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_Gender) ?? ""
        return str.count > 0 ? str : ""
    }

    //1 2
    class func getUserType() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_UserType) ?? ""
        return str.count > 0 ? str : ""
    }

    //3-揽件员,4-分拣员,5-派件员,6-app用户,7-app大客户
    class func getRoleType() -> Array<Any> {
        let devsn:Array = UserDefaults.standard.array(forKey: ActionHeader.UserDefault_RoleType) ?? []
        return devsn.count > 0 ? devsn : []
    }

    class func getNiceName() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_NiceName) ?? ""
        return str.count > 0 ? str : ""
    }

    class func getUserName() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_UserName) ?? ""
        return str.count > 0 ? str : ""
    }

    class func getStaffCode() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_StaffCode) ?? ""
        return str.count > 0 ? str : ""
    }

    class func getCompanyCode() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_CompanyCode) ?? ""
        return str.count > 0 ? str : ""
    }

    class func getPhoneCode() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_PhoneCode) ?? ""
        return str.count > 0 ? str : ""
    }

    class func getToken() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_Token) ?? ""
        return str.count > 0 ? str : ""
    }

    class func tokenHead() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_TokenHead) ?? ""
        return str.count > 0 ? str : ""
    }

    class func tokenrefresh() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_Tokenrefresh) ?? ""

        return str.count > 0 ? str : ""
    }

    class func tokenexpiryDuratio() -> String {
        let str = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_TokenexpiryDuration) ?? ""
        return str.count > 0 ? str  : ""
    }

    class func tokenexResTimer() -> String {
        let str:String = UserDefaults.standard.string(forKey: ActionHeader.UserDefault_TokenexResTimer) ?? ""
        return str.count > 0 ? str  : ""
    }

    class func setUserType(roleType:String) {
        UserDefaults.standard.setValue(roleType, forKey: ActionHeader.UserDefault_UserType) 
    }

    class func saveUser(jsonDic:JSON) {
        
        let resTimer = jsonDic["resTime"].description
        let dataDic1 = jsonDic["data"].description
        let jsonData = dataDic1.data(using: String.Encoding.utf8, allowLossyConversion: false)!
        let userDic = try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? [String:Any]
                
        guard (userDic?["accessToken"]) != nil else {return}
            
        let token:String = userDic?["accessToken"] as! String
        var tokenHead:String = userDic?["tokenType"] as! String
        let refreshToken:String = userDic?["refreshToken"] as! String
        let expiryDuration = String(userDic?["expiryDuration"] as! Int)
        
        let str:NSMutableString = NSMutableString.init(string: tokenHead)
        tokenHead = str.replacingOccurrences(of: " ", with: "")
        UserDefaults.standard.setValue(token, forKey: ActionHeader.UserDefault_Token)
        UserDefaults.standard.setValue(tokenHead, forKey: ActionHeader.UserDefault_TokenHead)
        UserDefaults.standard.setValue(refreshToken, forKey: ActionHeader.UserDefault_Tokenrefresh)
        UserDefaults.standard.setValue(expiryDuration, forKey: ActionHeader.UserDefault_TokenexpiryDuration)
        UserDefaults.standard.setValue(NSString.init(string: resTimer), forKey: ActionHeader.UserDefault_TokenexResTimer)
    }

}
